package viewer.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.AccessibleRole;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import sun.applet.AppletPanel;
import viewer.ui.control.SearchBox;
import viewer.ui.control.TitledToolBar;

public class Tester extends Application {
	boolean isClickedInAPane = false;
	Scene scene;
	
	public static void main(String[] args) {
		Application.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		BorderPane pane = new BorderPane();

		BorderPane apane = new BorderPane();
		apane.setPrefSize(50, 50);
		apane.setBackground(new Background(
				new BackgroundFill(Color.LIGHTBLUE, CornerRadii.EMPTY, new Insets(0))));

		Button b = new Button("Test");

		apane.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				Tester.this.isClickedInAPane = true;
				System.out.println("apane clicked!");

			}
		});

		pane.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println(event.getPickResult());

				System.out.println(event.isConsumed());
				if (Tester.this.isClickedInAPane) {
					System.out.println("i see!!!");
				} else {
					Tester.this.isClickedInAPane = false;
					System.out.println("button clicked!");
				}

			}
		});

		b.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("button clicked!");

			}
		});

		TitledToolBar toolBar = new TitledToolBar();

		Button b1 = new Button("", new ImageView(new Image("/icons/delete.png", 16, 16, true, true)));
		b1.setId("1");
		b1.getStyleClass().add("left-pill");
		b1.setPrefSize(30, 30);
		Button b2 = new Button("2");
		b2.getStyleClass().add("center-pill");
		b2.setPrefSize(30, 30);
		Button b3 = new Button("3");
		b3.getStyleClass().add("right-pill");
		b3.setPrefSize(30, 30);

        HBox navButtons = new HBox(0,b1,b2,b3);

		toolBar.addLeftItems(navButtons);
		ProgressIndicator pi = new ProgressIndicator();
		pi.setPrefSize(30, 30);
		toolBar.addRightItems(new SearchBox(), pi);

		pane.setTop(toolBar);

		scene = new Scene(pane, 800, 600);
		setStylesheets();
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
    private void setStylesheets() {
        final String EXTERNAL_STYLESHEET = "http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600";
        scene.getStylesheets().setAll("/ensemble/EnsembleStylesCommon.css");
        Thread backgroundThread = new Thread(() -> {
            try {
                URL url = new URL(EXTERNAL_STYLESHEET);
                try (
                        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
                        Reader newReader = Channels.newReader(rbc, "ISO-8859-1");
                        BufferedReader bufferedReader = new BufferedReader(newReader)
                        ) {
                    // Checking whether we can read a line from this url
                    // without exception
                    bufferedReader.readLine();
                }
                Platform.runLater(() -> {
                    // when succeeded add this stylesheet to the scene
                    scene.getStylesheets().add(EXTERNAL_STYLESHEET);
                });
            }catch (MalformedURLException ex) {
                Logger.getLogger(Tester.class.getName()).log(Level.FINE, "Failed to load external stylesheet", ex);
            }catch (IOException ex) {
                    Logger.getLogger(Tester.class.getName()).log(Level.FINE, "Failed to load external stylesheet", ex);
                }
        }, "Trying to reach external styleshet");
        backgroundThread.setDaemon(true);
        backgroundThread.start();
    }


}
